
package member
import (
	"github.com/astaxie/beego/orm"
	"strings"
	"errors"
	"dhfshop/models"
)

type TtMember struct {
	Id         int64  `orm:"column(id);pk;auto" json:"id" form:"id"`
    PartnerId  int64  `orm:"column(partner_id);index" json:"partner_id" form:"partner_id"` 
	AppId      string `orm:"column(app_id);size(32);index" json:"app_id"`                   
	Openid     string `orm:"column(open_id);size(190);index" json:"open_id"`
	Nickname   string `orm:"column(nick_name)" json:"nick_name"` //
	Avatar     string `orm:"column(avatarUrl);type(text)" json:"avatarUrl"` //
	Province   string `orm:"column(province)" json:"province"`  //省
	City       string `orm:"column(city)" json:"city"`          //城市
	Country    string `orm:"column(country)" json:"country"`    //国家
	Gender     int    `orm:"column(gender)" json:"gender" form:"gender"` //
	Language   string `orm:"column(language)" json:"language" form:"language"` 
	SessionKey string `orm:"column(session_key)" json:"session_key"` //
	AnonymousOpenid string `orm:"column(anony_openid)" json:"anony_openid"` //
	MemberId  int64  `orm:"column(member_id);index" json:"member_id" form:"member_id"` 
}

func init() {
    orm.RegisterModel(new(TtMember))   
}

func AddTtMember(m *TtMember) (id int64, err error) {
	o := models.GetDefaultDB()
	id, err = o.Insert(m)
	return
}

func GetTtMemberByMemberId(memberId int64) (v *TtMember, err error) {
	o := models.GetDefaultDB()
	v = &TtMember{MemberId: memberId}
	if err = o.Read(v, "MemberId"); err == nil {
		return v, nil
	}
	return nil, err
}


func GetAllTtMembers(appIds []string,query map[string]string,fields []string,order_by map[string]string,offset int,limit int)([]*TtMember,int64,error){
	o := models.GetDefaultDB()
	qs := o.QueryTable(new(TtMember))
	for k, v := range query {
		k = strings.Replace(k, ".", "__", -1)
		if ok := strings.Contains(v, "|"); ok {
		    valueArr := strings.Split(v, "|")
			qs = qs.Filter(k, valueArr)
		}else{
		    qs = qs.Filter(k, v)
		}	
	}
	if len(appIds) > 0 {
        qs = qs.Filter("app_id__in", appIds)
    }
	cnt, err := qs.Count()
	// order by:
	var sortFields []string
	for k, v := range order_by {
		orderby := ""
		if v == "desc" {
			orderby = "-" + k
		} else if v == "asc" {
			orderby = k
		} else {
			return nil, 0, errors.New("Error: Invalid order. Must be either [asc|desc]")
		}
		sortFields = append(sortFields, orderby)
	}
	qs = qs.OrderBy(sortFields...)
	var lists []*TtMember
	if _, err = qs.Limit(limit, offset).All(&lists, fields...); err == nil {
		return lists, cnt, nil
	}
	return nil, 0, err
}

func GetTtMemberByOpenId(openId string) (member *TtMember, err error) {
	o := models.GetDefaultDB()
	member = &TtMember{Openid:openId}   //多个的话,read中需要指明列
	if err = o.Read(member,"Openid"); err != nil {
		return member, err
	}
	return member, nil
}

// 更新会员
func UpdateTtMemberById(m *TtMember, k ...string) (err error) {
	o := models.GetDefaultDB()
	v := TtMember{MemberId: m.MemberId}
	if err = o.Read(&v); err == nil {
		if _, err = o.Update(m, k...); err != nil {
			return err
		}
	}
	return
}

func UpdateTtMemberByOpenId(m *TtMember, k ...string) (err error) {
	o := models.GetDefaultDB()
	v := TtMember{Openid: m.Openid}
	if err = o.Read(&v, "Openid"); err == nil {
		m.MemberId = v.MemberId
		if _, err = o.Update(m, k...); err != nil {
			return err
		}
	}
	return
}

func UpdateTtMemberByMemberId(m *TtMember, k ...string) (err error) {
	o := models.GetDefaultDB()
	v := TtMember{MemberId: m.MemberId}
	if err = o.Read(&v, "MemberId"); err == nil {
		m.Id = v.Id
		if _, err = o.Update(m, k...); err != nil {
			return err
		}
	}
	return
}

